package com.sen.rbac.controller;

import cn.hutool.core.util.StrUtil;
import com.sen.common.common.bo.SysResourceBO;
import com.sen.common.common.controller.BaseController;
import com.sen.common.common.response.MessageResult;
import com.sen.common.common.util.CheckUtils;
import com.sen.rbac.constant.RbacConstant;
import com.sen.rbac.dto.SysResourceDto;
import com.sen.rbac.feign.RbacResourceFeign;
import com.sen.rbac.service.ResourceService;
import com.sen.rbac.service.RoleService;
import com.sen.rbac.vo.SysResourceVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author esjiang
 */
@RestController
@RequestMapping("/resource")
public class ResourceController extends BaseController implements RbacResourceFeign {

    @Resource
    ResourceService resourceService ;

    @Resource
    RoleService roleService ;

    /**
     * 新增
     * @param sysResourceDto
     * @return
     */
    @PostMapping("/add")
    public MessageResult add(@RequestBody SysResourceDto sysResourceDto){
        return crud(sysResourceDto) ;
    }

    /**
     * 修改
     * @param sysResourceDto
     * @return
     */
    @PostMapping("/update")
    public MessageResult update(@RequestBody SysResourceDto sysResourceDto){
        return crud(sysResourceDto) ;
    }

    /**
     * 删除
     * @param sysResourceDto
     * @return
     */
    @PostMapping("/del")
    public MessageResult delete(@RequestBody SysResourceDto sysResourceDto){
        return crud(sysResourceDto) ;
    }

    /**
     * 查询
     * @param sysResourceDto
     * @return
     */
    @PostMapping("/list")
    public MessageResult list(@RequestBody SysResourceDto sysResourceDto){
        return crud(sysResourceDto);
    }

    /**
     * 增删改查
     * @param sysResourceDto
     * @return
     */
    public MessageResult crud(@RequestBody SysResourceDto sysResourceDto){
        return exec("CRUD资源",sysResourceDto,false,()->{
            CheckUtils.checkNotNull(sysResourceDto,"operationType");
            String operationType = sysResourceDto.getOperationType();
            if (StrUtil.equals(operationType, RbacConstant.ADD) ){
                CheckUtils.checkNotNull(sysResourceDto,"parentCode","name","type","sort");
            }
            if (StrUtil.equals(operationType,RbacConstant.UPD)){
                CheckUtils.checkNotNull(sysResourceDto,"code");
            }
            if (StrUtil.equals(operationType, RbacConstant.DEL)){
                CheckUtils.checkNotNull(sysResourceDto,"code");
            }
            if (StrUtil.equals(operationType,RbacConstant.LIST)){
                CheckUtils.checkNotNull(sysResourceDto,"systemRootCode");
            }
            return resourceService.crudResource(sysResourceDto);
        });
    }

    /***
     * 根据账户名查询资源信息
     * @param userName
     * @return
     */
    @Override
    @RequestMapping("/findResourceByUserNameForGateway")
    public MessageResult findResourceByUserNameForGateway(@RequestParam("userName") String userName) {
        return exec("根据账户名查询权限",userName,false,()->{
            List<SysResourceBO> authorityBOS = roleService.getResourcesByUserNameForGateway(userName);
            return MessageResult.success(authorityBOS) ;
        });
    }

    /**
     * 根据角色查询资源信息
     * @param code
     * @return
     */
    @RequestMapping("/findResourceByRoleCode")
    public MessageResult findResourceByRoleCode(String code){
        return exec("根据角色查询资源信息",code,()->{
            CheckUtils.checkNotNull(code);
            List<SysResourceVO> authorityByRoleCode = roleService.findResourceByRoleCode(code);
            return MessageResult.success(authorityByRoleCode) ;
        });
    }

}
